//
//  XJLocalTravelLoadDataVC.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/7/3.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "XJLocalTravelLoadDataVC.h"

//ViewController
#import "XJLocalTravelViewController.h" //网易效果vc (vendor)
#import "XJLocalTravelDetailVC.h" // 具体要展示的数据vc
#import "XJSelectCityViewController.h" // 6-4选择城市
#import "XJSearchTieZiViewController.h"// 6-5搜索帖子

// category
#import "UIButton+ImgWithText.h"

//model
#import "XJCityCtyModel.h"

@interface XJLocalTravelLoadDataVC ()
/**定义成属性方便外面改变其值*/
@property (nonatomic, strong)UIButton *cityButton;

@end

@implementation XJLocalTravelLoadDataVC

#pragma mark -  二 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self setupNav];
    
    [self requestInvitationCityPage];
}

#pragma mark - 设置导航条navigationItem.titleView
- (void)setupNav {
    
//    UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 30)];
//    redView.backgroundColor = [UIColor redColor];
//    [self.navigationController.navigationBar addSubview:redView];
    
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, 44)];
    self.cityButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [self.cityButton buttonWithTitle:@"杭州" titleFont:[UIFont systemFontOfSize:17.0] titleColor:[UIColor blackColor] imageType:(ImageTypeImg) imageName:@"direction_down_black" withTarget:self action:@selector(cityButtonBlock) style:(ImgTextBtnStyleRight) margin:30];
    self.cityButton.frame = CGRectMake(0, 0, 150, 44);
    [titleView addSubview:self.cityButton];
    
    self.navigationItem.titleView = titleView;
    
    // 搜索
    UIBarButtonItem *rightSearchItem =  [[UIBarButtonItem alloc] initWithBarButtonSystemItem:(UIBarButtonSystemItemSearch) target:self action:@selector(navBarSearchAction)];
    self.navigationItem.rightBarButtonItem = rightSearchItem;
}

- (void)cityButtonBlock {
    XJLog(@"跳转选择城市vc");
    
    XJSelectCityViewController *cityVC = [[XJSelectCityViewController alloc] init];
    cityVC.selectedCityBlock = ^(NSString *selectedCity) {
        [self.cityButton setTitle:selectedCity forState:(UIControlStateNormal)];
    };
    
    [self.navigationController pushViewController:cityVC animated:YES];
}

- (void)navBarSearchAction{
    XJLog(@"搜索");
    XJSearchTieZiViewController *searchTieZiVC = [[XJSearchTieZiViewController alloc] init];
    searchTieZiVC.city = self.cityButton.titleLabel.text;
    
    [self.navigationController pushViewController:searchTieZiVC animated:YES];
}


//实现网易效果
- (void)setupPageVCWithImgArr:(NSArray *)imgArr vcArr:(NSArray *)vcArr titleArr:(NSArray *)titleArr {
    XJLocalTravelViewController *travelVC = [XJLocalTravelViewController suspendCenterPageVCWithBannerImgArr:imgArr vcsArr:vcArr titleArr:titleArr];
    
    [travelVC addSelfToParentViewController:self];
}

#pragma mark - 三 网络请求

/**请求城市帖子所有分类的数据*/
- (void)requestInvitationCityPage {
    
    if ([NSString isEmptyString:self.localCity]) {
        self.localCity = @"杭州";
    }
    
    XJLog(@"确保self.localCity不为空:%@",self.localCity);
    
    
    [XJHttpTool requestInvitationCityPageCity:self.localCity successH:^(id responseObjc) {
        NSString *code = [NSString stringWithFormat:@"%@",responseObjc[@"code"]];
        
        NSMutableArray *titleArr = [NSMutableArray array]; //+1  @"全部"
        [titleArr addObject:@"全部"];
        
        NSMutableArray *bannerImgArr = [NSMutableArray array];
        
        NSMutableArray *cityCategoryArr = [NSMutableArray array]; //+1 @"全部"
        XJCityCtyModel *allModel = [[XJCityCtyModel alloc] init];
        allModel.name = @"全部";
        allModel.ctyId = @"0";
        [cityCategoryArr addObject:allModel];
        
        
        if ([code isEqualToString:@"0"]) {
            NSDictionary *contentDic = responseObjc[@"data"][@"content"];
            
            for (NSDictionary *bannerDic in contentDic[@"banners"]) {
                NSString *imgUrl = bannerDic[@"image"];
                [bannerImgArr addObject:imgUrl];
            }
            
            for (NSDictionary *cityCtyDic in contentDic[@"invitation_city_categories"]) {
                XJCityCtyModel *cityCtyModel = [XJCityCtyModel mj_objectWithKeyValues:cityCtyDic];
                [cityCategoryArr addObject:cityCtyModel];
                [titleArr addObject:cityCtyModel.name];
            }
        }
        
        //主线程中刷新
        dispatch_async(dispatch_get_main_queue(), ^{
            
            NSArray *vcArr = [self dynamicCreateVcsWithArr:cityCategoryArr];
            [self setupPageVCWithImgArr:bannerImgArr vcArr:vcArr titleArr:titleArr];
        });
        
    } failH:^(NSError *error) {
        [JRToast showWithText:[error localizedDescription]];
    }];
}


#pragma mark - 四  Private method
//根据请求的数据动态创建子控制器
- (NSArray *)dynamicCreateVcsWithArr:(NSArray <XJCityCtyModel *>*)arr {
    
    if ([NSString isEmptyString:self.localCity]) {
        self.localCity = @"杭州";
    }
    
    NSMutableArray *vcArr = [NSMutableArray array];
    
    for (NSInteger i = 0; i<arr.count; i++) {
        
        XJCityCtyModel *model = arr[i];
        
        XJLocalTravelDetailVC *localDetailVC = [[XJLocalTravelDetailVC alloc] init];
        
        localDetailVC.city = self.localCity; //请求参数1
        localDetailVC.cityCategoryId = model.ctyId;//请求参数2
        
        localDetailVC.cellTitle = model.name;
        
        [vcArr addObject:localDetailVC];
    }
    
    return [vcArr copy];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
